package com.zzl.myblog.View.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


import com.zzl.myblog.R;
import com.zzl.myblog.View.Fragment.WebFragment;


public class WebActivity extends AppCompatActivity {
    //文章链接
    String webURL;

    //用户名]
    String username;

    String TAG = "WebActivity";
    
    WebFragment webFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        //拿到username、webUrl
        Intent intent = getIntent();
        webURL = intent.getStringExtra("webURL");
        username = intent.getStringExtra("username");
        
        Log.d(TAG, "onCreate: username "+username);

        // 拿到一个fragment填充到布局中
        if (webFragment == null) {
            Fragment targetFragment = WebFragment.newInstance(webURL,username);
            // 展示到指定Fragment布局中
            replaceFragment(targetFragment);
        }

    }

    /**
     * 自定义方法
     * https://blog.csdn.net/look_Future/article/details/79658861
     * Fragment的生命周期和FragmentTransaction的主要方法
     * - 增减fragment到队列中
     * - 并显示该fragment
     * @param fragment
     */
    private void replaceFragment(Fragment fragment) {
        /**
            将frament添加到队列中，并显示当前的fragment
         */
        FragmentManager fragmentManager = getSupportFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        //add(id, fragment) —— 增加framgent到队列中，并显示该fragment到指定布局中。
        fragmentTransaction.add(R.id.web_fragment, fragment);

        fragmentTransaction.commit();
    }
}